<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once(APPPATH . 'models/model_kontak.php');
require_once(APPPATH . 'models/model_grup.php');

define('debug_ekstrak', FALSE);

class Pdf_tajnid extends CI_Controller {

    public function index()
    {
    	$data['isi'] = '<div class="panel panel-primary"><div class="panel-heading"><h3 class="panel-title"> Ekstrak Kulit Pisang</h3></div><div class="panel-body table-responsive"><form action="'.site_url('/pdf_tajnid/proses').'" method="post" enctype="multipart/form-data"><input type="file" name="userfile" size="20" /><br /><br /><input type="submit" value="upload" class="btn btn-primary"/></form></div></div>';

		$this->load->view('theme/theme_kepala');
		$this->load->view('blank', $data);
		$this->load->view('theme/theme_kaki');
	}

	public function proses()
	{
		$config['upload_path'] = '/tmp/';
		$config['allowed_types'] = 'pdf';

		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload())
		{
			$error = $this->upload->display_errors();

	    	$data['isi'] = '<div class="panel panel-primary"><div class="panel-heading"><h3 class="panel-title"> Ekstrak Kulit Pisang</h3></div><div class="panel-body table-responsive">'.$error.'</div></div>';

			$this->load->view('theme/theme_kepala');
			$this->load->view('blank', $data);
			$this->load->view('theme/theme_kaki');
		}
		else
		{
			$file = $this->upload->data();
			
			$debug = $this->ekstrak($file['full_path']);

			$data['isi'] = '<div class="panel panel-primary"><div class="panel-heading"><h3 class="panel-title"> Ekstrak Kulit Pisang</h3></div><div class="panel-body table-responsive"><p>Hasil ekstraksi:</p><pre>'.$debug.'</pre></div></div>';

			$this->load->view('theme/theme_kepala');
			$this->load->view('blank', $data);
			$this->load->view('theme/theme_kaki');
		}
	}

	private function ekstrak($filename)
	{
    	// membuat data grup

    	$grup_array = model_grup::read();

    	$grup_huruf_id = array();

    	foreach ($grup_array as $grup)
    		$grup_huruf_id[$grup->get_keterangan()] = $grup->get_id();

    	// membaca file pdf

    	set_time_limit(1200);

    	system('chmod 0777 '.$filename);
	    $content = shell_exec('pdftotext -fixed 2000 '.$filename.' - ');
	    system('rm -rf '.$filename);


	    $separator = "\r\n";

	    // inisialisasi

	    $tz = " ";
	    $line = strtok($content, $separator);

	    $i=0;

	    while ($line !== false) {
	    	
	    	$char_1 = ord($line[0]);

	    	// jika karakter pertama adalah angka
	        if (($char_1>=48) && ($char_1<=57) && (strlen($line)==5)){

	        	$i++;

	        	$data= new stdClass();

	        	// kalau angka berarti nomor AIMS
	        	$data->pbk_id = $line;
	        	// baris selanjutnya adalah nama
	        	$data->nama = strtok( $separator );

	        	$nomor = array();

	        	// beberapa baris selanjutnya adalah nomor telepon
	        	while (($line=strtok($separator)) && ($char_1=ord($line[0])) && (($char_1>=48 && $char_1<=57) || $char_1==43 || $char_1==39)) {

	        		// membersihkan

					$line = str_replace(" ", "", $line);
					$line = str_replace("'", "", $line);
	        		$line = str_replace("-", "", $line);
	        		$line = str_replace("+", "", $line);

	        		$nomor[] = $line;
	        	}

	        	if (strlen($line)<=3)
	        		$rel = $line;
	        	else
	        		$rel = strtok( $separator );

	        	$tz  = strtok( $separator );

	        	if (strlen($tz)>0 && $tz[strlen($tz)-1]=='M')
	        		$nomor_wasiyat = strtok( $separator );

        		$data->nomor = implode(', ', $nomor);

				$debug .= ("\n$i -----------------------------------\n");
	        	$debug .= var_export($data, true);
	        	$debug .= ("\n-----------------------------------\n");

        		debug_ekstrak || model_kontak::create($data);
        		
				// menambahkan relasi kontak-grup
				model_crud::$tabel="pbk_relasi";
				model_crud::$tabel_join=array();

				$relasi = array('pbk_id' => $data->pbk_id);

				$relasi['grup_id'] = 1;
				debug_ekstrak || model_crud::create($relasi);

				$relasi['grup_id'] = $grup_huruf_id[$tz[0]];
				debug_ekstrak || model_crud::create($relasi);
				
	        }
	        else
	        {
				$line = strtok( $separator );
	        }
	    }

	    return $debug;
	}
}